package com.asia5b.a5mall.member.adapter

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.appcompat.app.AppCompatActivity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import com.asia5b.a5mall.member.R
import com.asia5b.a5mall.middleware.utils.WImageLoader
import com.asia5b.a5mall.middleware.utils.openPicture
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia

class UploadAdapter(var list: ArrayList<LocalMedia>) :
        BaseQuickAdapter<LocalMedia, BaseViewHolder>(R.layout.item_upload_adapter, list) {
    init {
        addChildClickViewIds(R.id.iv_upload_delete)
    }

    var selectionMedia: ArrayList<LocalMedia>? = null
    var uploadPosition: Int = 0

    override fun convert(helper: BaseViewHolder, item: LocalMedia) {
        val ll_upload_add = helper.getView<LinearLayout>(R.id.ll_upload_add)
        val cons_upload_img_vessel = helper.getView<ConstraintLayout>(R.id.cons_upload_img_vessel)
        val cons_upload_vessel = helper.getView<ConstraintLayout>(R.id.cons_upload_vessel)
        val iv_upload_img = helper.getView<ImageView>(R.id.iv_upload_img)
        val iv_upload_delete = helper.getView<ImageView>(R.id.iv_upload_delete)
        //重新给宽度
        if (item.position == -1) {//添加按钮
            ll_upload_add.visibility = View.VISIBLE
            cons_upload_img_vessel.visibility = View.GONE
            ll_upload_add.setOnClickListener {
                if (context is AppCompatActivity) {
                    val mActivity = context as AppCompatActivity
//                    PictureSelector.create(mActivity)
//                            .openGallery(PictureMimeType.ofImage())
//                            .theme(R.style.picture_default_style)// 主题样式设置 具体参考 values/styles
//                            .previewImage(true)// 是否可预览图片
//                            .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选
//                            .compress(true)// 是否压缩
//                            .openClickSound(false)// 是否开启点击声音
//                            .maxSelectNum(5)
//                            .selectionMedia(selectionMedia)
//                            .forResult(uploadPosition);//结果回调onActivityResult code
                    mActivity.openPicture(5, isSingle = false, selectMedia = selectionMedia,requestCode =uploadPosition )
                }
            }
        } else {
            ll_upload_add.visibility = View.GONE
            cons_upload_img_vessel.visibility = View.VISIBLE
            WImageLoader.displayRoundImage(item.compressPath, iv_upload_img, 5)
        }

    }
}